// orderinfo.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
//获取应用实例
var app = getApp();
var baseHandle = require("../../template/baseHandle.js");
var commHandle = require("../../template/commHandle.js");
var cusmallToken = wx.getStorageSync('cusmallToken');

Page(Object.assign({}, commHandle, baseHandle, {

    /**
     * 页面的初始数据
     */
    data: {
      id: "",
      app: app,
      mallSiteTpl: "",
      getSelfAddr: {},
      addressTitle: "收货地址：",
      orderId: "",
      sku: "",
      orderData: null,
      btnLoading: true,
      fromShopingCart: false,
      cartIds: "",
      goodsList: [],
      addressInfo: null,
      orderStatus: "",
      tel: "",
      enableDepositSW: false,
      areaId: "",
      inputContent: { remark: "" },
      totalPrice: 0,
      allPrice: 0,
      deliveryPrice: 0,
      staticResPath: cf.config.staticResPath,
      userImagePath: cf.config.userImagePath,
      goodsCount: 0,
      cardList: [],
      couponList: [],
      showCouponList: false,
      showCardList: false,
      showExpTime: false,
      selectedExpTime: 0,
      todayWeek: "",
      orderType: "",
      prepareOrderVo: {},
      pointsToMoney: 0,
      selectedDineWay: 1,
      enablePointsChe: false,
      discountCount: 0,
      calcFinPrice: 0,
      showGoodsCount: false,
      showGoodsPrice: false,
      goodsType: '',
      verType: "order",
      isExtra: false,
      extraItem: [],
      imgObj: {},
      orderDeInf: [],
      inputText: [],
      refundMoney: "",
      refundType: "",
      accountPackagePrivilege: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      wx.hideShareMenu();
      cusmallToken = wx.getStorageSync('cusmallToken');
      var parse = JSON.parse;
      wx.hideShareMenu();
      var pageTitle = "订单详情";
      if (options.orderid) {
        this.setData({ orderId: options.orderid });
      }

      if (options.orderid) {
        this.fetchOrderData(this.afterGoodsLoaded);
      }
    },

    // 从订单获取商品信息
    fetchOrderData: function (cb) {
      //this.mallSiteFindConfig(mallSiteId);//查询物流配置
      var that = this;
      wx.showLoading({
        title: '加载中',
      });
      wx.request({
        url: cf.config.pageDomain + '/applet/mobile/visitcard/order/findEmployeeOrderDetail',
        data: {
          cusmallToken: cusmallToken,
          id: that.data.orderId
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.ret == 0) {
            var parOrder = res.data.model.order;
            util.processOrderData(parOrder);
            that.setData({ totalPrice: parOrder.goodsAmountTotal });
            that.setData({ goodsList: parOrder.orderItems });
            that.setData({ orderData: parOrder });
            that.setData({ goodsCount: parOrder.goodsCount });
            that.setData({
              addressInfo: {
                "userName": parOrder.nickname,
                "tel": parOrder.tel,
                "areaName": "",
                "address": (parOrder.address && JSON.parse(parOrder.address).address) || "",
              }
            });
            that.setData({ orderStatus: util.getOrderStatus(parOrder) });
            that.setData({
              orderType: parOrder.orderType
            });
            that.setData({
              calcFinPrice: parOrder.orderAmountTotal,
              orderDeInf: JSON.parse(parOrder.extraInfo)
            });

            that.setData({ deliveryPrice: that.data.orderData.deliveryFee });
            if (that.data.orderData.sellerDiscountMoney) {
              that.setData({ sellerDiscountMoney: that.data.orderData.sellerDiscountMoney });
            }

            //退款金额
            if (2 == parOrder.refundType) {
              var refundDetail = JSON.parse(parOrder.refundDetail || "{}");
              that.setData({
                refundMoney: refundDetail.refund_actual + refundDetail.refund_deposit,
                refundType: "部分"
              });
            } else {
              that.setData({
                refundMoney: parOrder.actualPrice + parOrder.depositPrice,
                refundType: "全部"
              });
            }

            wx.hideLoading();
            cb && cb();
          } else {
            wx.hideLoading();
            wx.showModal({
              title: '获取订单信息异常',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })

    },

    

    toGoodsDetail(e) {
      var ctx = this;
      var gId = e.currentTarget.dataset.gid;
      var orderData = ctx.data.orderData;
      wx.navigateTo({
        url: '/pages/shopping_mall/detail/detail?id=' + gId,
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角
     */
    onShareAppMessage: function () {

    }
  }))